package per.sunmes.lesrb.manager;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

import per.sunmes.lesrb.apis.SaveDataOperation;
import per.sunmes.lesrb.data.DefaultSaveDataOperation;
import per.sunmes.lesrb.data.SaveData;

public class UserM {

	private static UserM _i;

	public static SaveDataOperation saveDataOperation;

	private static UserM I() {
		if (_i == null) {
			_i = new UserM();
		}
		return _i;
	}

	public static void dispose() {
		if (_i != null) {
			_i._dispose();
			_i = null;
		}
	}

	public static void save() {
		I()._save();
	}

	public static SaveData data() {
		return I().saveData;
	}

	public static String saveFileName = "savedata.dat";

	public static FileHandle getSaveFile() {
		return Gdx.files.absolute(Gdx.files.getLocalStoragePath() + "/" + saveFileName);
	}

	////////////////
	SaveData saveData;

	private UserM() {
		if (saveDataOperation == null) {
			saveDataOperation = new DefaultSaveDataOperation();
		}
		load();
	}

	private void load() {
		FileHandle saveFile = getSaveFile();
		if (!saveFile.exists()) {
			saveData = new SaveData();
			_save();
		} else {
			saveData = saveDataOperation.load(saveFile.read());
		}
	}

	private void _save() {
		saveDataOperation.save(saveData, getSaveFile().write(false));
	}

	private void _dispose() {

	}

}
